package com.shanghaionstar.notification.util;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.List;

/**
 * SharePrefence文件工具类
 * @author changtaoxie
 *
 */
public class ShareUtils {
	public static final int DEFVALUE_FOR_NONOEPRREFERENCE = 0;
	public static final float DEFVALUE_FOR_NONOEPRREFERENCE_FLOAT = -1.0f;

	/**
	 * 将对应的String值写入SharedPreferences
	 */
	public static void put(String name, String value, Context context) {
		SharedPreferences settings = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		settings.edit().putString(name, value).commit();
	}

	/**
	 * 从SharedPreferences中读取对应的String值。
	 * 
	 * @return null 没有此项值
	 */
	public static String getString(String name, Context context) {
		SharedPreferences sp = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		return sp.getString(name, "");
	}

	public static void remove(Context context, String name) {
		SharedPreferences sp = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		sp.edit().remove(name).commit();
	}

	/**
	 * 将对应的Int值写入SharedPreferences
	 */
	public static void put(String name, int value, Context context) {
		SharedPreferences settings = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		settings.edit().putInt(name, value).commit();
	}

	/**
	 * 将对应的Long值写入SharedPreferences
	 */
	public static void put(String name, long value, Context context) {
		SharedPreferences settings = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		settings.edit().putLong(name, value).commit();
	}

	/**
	 * 将对应的Float值写入SharedPreferences
	 */
	public static void put(String name, float value, Context context) {
		SharedPreferences settings = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		settings.edit().putFloat(name, value).commit();
	}

	/**
	 * 将对应的bolean值写入SharedPreferences
	 */
	public static void put(String name, Boolean value, Context context) {
		SharedPreferences settings = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		settings.edit().putBoolean(name, value).commit();
	}

	/**
	 * 将对应的bolean值写入SharedPreferences
	 */
	public static boolean getBoolean(String name, Context context) {
		SharedPreferences settings = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		return settings.getBoolean(name, false);
	}

	/**
	 * 从SharedPreferences中读取对应的Int值。
	 * 
	 * @return -1 没有此项值
	 */
	public static int getInt(String name, Context context) {
		SharedPreferences settings = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		return settings.getInt(name, DEFVALUE_FOR_NONOEPRREFERENCE);
	}

	/**
	 * 从SharedPreferences中读取对应的Long值。
	 * 
	 * @return -1 没有此项值
	 */
	public static long getLong(String name, Context context) {
		SharedPreferences settings = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		return settings.getLong(name, DEFVALUE_FOR_NONOEPRREFERENCE);
	}

	/**
	 * 从SharedPreferences中读取对应的Long值。
	 * 
	 * @return -1 没有此项值
	 */
	public static float getFloat(String name, Context context) {
		SharedPreferences settings = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		return settings.getFloat(name, DEFVALUE_FOR_NONOEPRREFERENCE_FLOAT);
	}

	/**
	 * 将对应的Set<String>值写入SharedPreferences
	 */
	public static void put(String name, List<String> value, Context context) {
		if (value == null || value.size() <= 0) {
			return;
		}
		SharedPreferences sp = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		int count = value.size();
		String target = value.get(0);
		for (int i = 1; i < count; ++i) {
			target += ("|" + value.get(i));
		}
		editor.putString(name, target).commit();
	}

	/**
	 * 从SharedPreferences中读取对应的String[]值。
	 * 
	 * @return null 没有对应的值
	 */
	public static String[] getStringArray(String name, Context context) {
		SharedPreferences sp = context.getSharedPreferences("OnstarSetting",
				Context.MODE_PRIVATE);
		String values = sp.getString(name, null);
		if (values != null) {
			return values.split("\\|");
		}
		return null;
	}

}
